const { connection } = require('./index');
const { isAccountHasPortrait } = require('./account');
const { server } = require('../config/config.default');

const getCommentListOfPost = (postId) => {
  return new Promise((resolve, reject) => {
    if (!postId) {
      reject({ message: 'Post is not found in your request.' });
    }
    connection.query(
      `SELECT * FROM comment WHERE post_id=${postId}`,
      (err, result) => {
        if (err) {
          reject({ message: 'Post is not found in your request.' });
        }
        result.map((item) => {
          isAccountHasPortrait(item.from_user).then(
            (r) => {
              if (r[0].has_portrait === '0') {
                item.user_ptofile = server.baseURL + '/portrait/' + 'default.png'
              } else {
                item.user_ptofile = server.baseURL + '/portrait/' + r[0].has_portrait
              }
            },
            (e) => {
              reject({ message: 'User_profile is not found in your request.' });
            }
          )
        })
        resolve(result);
      }
    );
  });
};

const getCommentById = (commentId) => {
  return new Promise((resolve, reject) => {
    if (!commentId) {
      reject({ message: 'Comment is not found in your request.' });
    }
    connection.query(
      `SELECT * FROM comment WHERE comment_id='${commentId}'`,
      (err, result) => {
        if (err) {
          reject({ message: 'Comment is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
};

const addComment = (info) => {
  const { post_id, from_user, comment_text, comment_likes } = info;
  const now_time = new Date();
  const sqlParams = [post_id, now_time, from_user, comment_text, comment_likes];

  console.log(sqlParams);
  return new Promise((resolve, reject) => {
    connection.query(
      'INSERT INTO comment (post_id, create_time, from_user, comment_text, comment_likes) VALUES(?,?,?,?,?)',
      sqlParams,
      (err, result) => {
        if (err) {
          reject({ message: 'Failed to add.' });
        }
        resolve(result);
      }
    );
  });
};

const likeComment = (commentId) => {
  return new Promise((resolve, reject) => {
    if (!commentId) {
      reject({ message: 'Comment is not found in your request.' });
    }

    connection.query(
      `UPDATE comment SET comment_likes = comment_likes+1 WHERE comment_id='${commentId}'`,
      function (err, result) {
        if (err) {
          reject({ message: 'Comment is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
};

const unlikeComment = (commentId) => {
  return new Promise((resolve, reject) => {
    if (!commentId) {
      reject({ message: 'Comment is not found in your request.' });
    }
    connection.query(
      `UPDATE comment set comment_likes = comment_likes - 1 WHERE comment_id='${commentId}'`,
      function (err, result) {
        if (err) {
          reject({ message: 'Comment is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
}

const deleteComment = (commentId) => {
  return new Promise((resolve, reject) => {
    if (!commentId) {
      reject({ message: 'Comment is not found in your request.' });
    }
    connection.query(
      `DELETE FROM comment where comment_id='${commentId}'`,
      function (err, result) {
        if (err) {
          reject({ message: 'Comment is not found in your request.' });
        }
        resolve(result);
      }
    );
  });
};

module.exports = {
  getCommentListOfPost,
  getCommentById,
  addComment,
  likeComment,
  unlikeComment,
  deleteComment
}
